package com.baiyun.Ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, MouseListener {
    //这里写有关游戏界面的
    //定义二维数组用来管理数据
    private int[][] data = new int[4][4];

    //定义用于判断的二维数组
    private int[][] recoverData = getRecoverData();

    //定义空白图片的索引
    private int x;
    private int y;


    //将路径写在成员位置，需要更换图片时只需要改变路径的头部

    private String pathTitle = getPathTitle();

    private int step;

    public GameJFrame() {
        //初始化界面
        initJFrame();

        //初始化菜单
        initJMenuBar();

        //打乱图片
        toRandomImage();

        //初始化图片
        initImage();

        //点X后让界面消失
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //保证界面不消失
        this.setVisible(true);
    }

    private String getPathTitle(){
        //创建Random对象，用于生成随机图片
        Random r = new Random();
        String[] image = {"animal", "girl", "sport"};
        int imageIndex = r.nextInt(3);
        int imagePart = 0;
        if (image[imageIndex].equals("animal")){
            imagePart = 8;
        } else if (image[imageIndex].equals("girl")) {
            if (LoginJFrame.getUsername().equals("lisi")){
                imagePart = r.nextInt(2) + 12;
            }else {
                imagePart = r.nextInt(11) + 1;
            }
        } else if (image[imageIndex].equals("sport")) {
            imagePart = 10;
        }
        return "image/" + image[imageIndex] + "/" + image[imageIndex] +
                (r.nextInt(imagePart) + 1) + "/";
    }

    private String getPathTitle(String object){
        Random r = new Random();
        step = 0;
        if (object.equals("animal")){
            return "image/animal/animal" + (r.nextInt(8) + 1) + "/";
        } else if (object.equals("girl")) {
            if (LoginJFrame.getUsername().equals("lisi")){
                return "image/girl/girl" + (r.nextInt(2) + 12) + "/";
            }else {
                return "image/girl/girl" + (r.nextInt(11) + 1) + "/";
            }
        } else if (object.equals("sport")) {
            return "image/sport/sport" + (r.nextInt(10) + 1) + "/";
        }
        return null;
    }

    private boolean checkVictory() {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data.length; j++) {
                if (recoverData[i][j] != data[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    private void toRandomImage() {
        Random r = new Random();
        int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        for (int i = 0; i < arr.length; i++) {
            int randomIndex = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i] = arr[randomIndex];
            arr[randomIndex] = temp;
        }

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = arr[i];


        }
    }

    private void initImage() {

        //清楚之前的图像
        this.getContentPane().removeAll();

        //加入计步器
        JLabel jLabelStep = new JLabel("步数：" + step);
        jLabelStep.setBounds(50, 30, 100, 20);
        this.getContentPane().add(jLabelStep);

        //判断胜利
        boolean flag = checkVictory();
        if (flag) {
            JLabel jLabelWin = new JLabel(new ImageIcon("image/win.png"));
            jLabelWin.setBounds(203, 283, 197, 73);
            this.getContentPane().add(jLabelWin);
        }
        for (int i = 0; i < data.length; i++) {

            for (int j = 0; j < data.length; j++) {
                //组成图片的相对地址
                //创建一个图片ImageIcon的对象
                String Filename = pathTitle + data[i][j] + ".jpg";
                ImageIcon image = new ImageIcon(Filename);

                //创建一个JLabel的对象（管理容器）
                JLabel jLabel = new JLabel(image);
                jLabel.setBounds(j * 105 + 83, i * 105 + 134, 105, 105);

                //给图片加边框
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));

                //把容器添加界面中
                this.add(jLabel);
            }

        }
        JLabel bg = new JLabel(new ImageIcon("image/background.png"));
        bg.setBounds(40, 40, 508, 560);
        this.getContentPane().add(bg);

        this.getContentPane().repaint();
    }

    JMenuItem replayItem;
    JMenuItem reLoginItem;
    JMenuItem closeItem;
    JMenuItem accountItem;

    //创建更换图片下的各个选项
    JMenuItem animal;
    JMenuItem charmingGirl;
    JMenuItem sport;

    private void initJMenuBar() {
        //用JMenuBar创建菜单
        JMenuBar jMenuBar = new JMenuBar();

        //创建JMenu对象
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");


        //创建更换图片功能
        JMenu changeImage = new JMenu("更换图片");

        //创建JMenuItem即每一个选项下的条目选项
        animal = new JMenuItem("动物");
        charmingGirl = new JMenuItem("美女");
        sport = new JMenuItem("运动");
        replayItem = new JMenuItem("重新游戏");
        reLoginItem = new JMenuItem("重新登录");
        closeItem = new JMenuItem("关闭游戏");
        accountItem = new JMenuItem("公众号");

        //将更换图片下的各个选项添加到changeImage中
        changeImage.add(animal);
        changeImage.add(charmingGirl);
        changeImage.add(sport);

        //将条目添加到JMenu中
        functionJMenu.add(changeImage);
        functionJMenu.add(replayItem);
        functionJMenu.add(reLoginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);

        //为更换图片下的各个选项添加事件
        animal.addMouseListener(this);
        charmingGirl.addMouseListener(this);
        sport.addMouseListener(this);


        replayItem.addMouseListener(this);
        reLoginItem.addMouseListener(this);
        closeItem.addMouseListener(this);

        accountItem.addMouseListener(this);

        //将两个JMenu添加到jMenuBar中
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);

        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        this.setLayout(null);
        //设置长宽高
        this.setSize(603, 680);
        //设置标题
        this.setTitle("拼图单机版 v1.0");
        //将界面置顶
        this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        //添加keyListener
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 65) {
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon(pathTitle + "all.jpg"));
            jLabel.setBounds(83, 134, 420, 420);
            this.add(jLabel);
            JLabel bg = new JLabel(new ImageIcon("image/background.png"));
            bg.setBounds(40, 40, 508, 560);
            this.getContentPane().add(bg);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (checkVictory()) {
            return;
        }

        //左：37  上：38 右：39  下：40
        int code = e.getKeyCode();
        System.out.println(code);
        if (code == 37) {
            System.out.println("向左移动");
            if (y >= 3) {
                System.out.println("右方没有方块可以移动了");
                return;
            }
            System.out.println(y);
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            step++;
            y++;
            initImage();
        } else if (code == 38) {
            System.out.println("向上移动");
            if (x >= 3) {
                x = 3;
                System.out.println("下方没有方块可以移动了");
                return;
            }
            System.out.println(x);
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0;
            step++;
            x++;
            initImage();
        } else if (code == 39) {
            System.out.println("向右移动");
            if (y == 0) {
                System.out.println("左方没有方块可以移动了");
                return;
            }
            System.out.println(y);
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            step++;
            y--;
            initImage();
        } else if (code == 40) {
            System.out.println("向下移动");
            if (x <= 0) {
                System.out.println("上方没有方块可以移动了");
                return;
            }
            System.out.println(x);
            data[x][y] = data[x - 1][y];
            data[x - 1][y] = 0;
            step++;
            x--;
            initImage();
        } else if (code == 87) {
            //将数组还原
            data = getRecoverData();
            x = 3;
            y = 3;
            initImage();
        } else if (code == 65) {
            initImage();
        }
    }

    private int[][] getRecoverData() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0};
        int[][] result = new int[4][4];
        for (int i = 0; i < arr.length; i++) {
            result[i / 4][i % 4] = arr[i];
        }
        return result;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if (source == replayItem) {
            step = 0;
            toRandomImage();
            initImage();
        } else if (source == reLoginItem) {
            //关闭当前界面
            this.setVisible(false);
            //创建登录对象
            new LoginJFrame();
        } else if (source == closeItem) {
            System.exit(0);
        } else if (source == accountItem) {
            //创建一个管理图片的对象
            JLabel jLabel = new JLabel(new ImageIcon("image/about.png"));
            //创建弹窗对象
            JDialog jDialog = new JDialog();
            //设置jLabel的大小位置
            jLabel.setBounds(0, 0, 258, 258);
            //添加jLabel到jDialog
            jDialog.getContentPane().add(jLabel);
            //设置弹窗的大小
            jDialog.setSize(344, 344);
            //保持弹窗置顶
            jDialog.setAlwaysOnTop(true);
            //让弹窗居中
            jDialog.setLocationRelativeTo(null);
            //让弹窗不关闭则无法进行操作下面的界面
            jDialog.setModal(true);
            //让弹窗显示出来
            jDialog.setVisible(true);
        } else if (source == animal) {
            pathTitle = getPathTitle("animal");
            toRandomImage();
            initImage();
        } else if (source == charmingGirl) {
            pathTitle = getPathTitle("girl");
            toRandomImage();
            initImage();
        } else if (source == sport) {
            pathTitle = getPathTitle("sport");
            toRandomImage();
            initImage();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
